<template>
  <!-- 添加或修改代理商用户对话框 -->
  <el-dialog :close-on-click-modal="false" :title="formType" :visible.sync="open" @close="cancel"
             width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="90px">
      <el-row v-if="formType == $formType.add">
        <el-col :span="12">
          <el-form-item label="登录手机" prop="username">
            <el-input v-model="form.username" :disabled="formType == $formType.edit" placeholder="请输入帐号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="初始密码">
            <el-input v-model="form.password" :type="formType == $formType.edit ? 'password':'text'" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="联系人" prop="agentName">
            <el-input v-model="form.agentName" placeholder="请输入联系人姓名或公司"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话手机" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入电话手机"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="直推比例" prop="directScale">
            <el-input-number v-model="form.directScale" :precision="2" :step="0.01" :max="100"
                             placeholder="请输入直推比例"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="间推比例" prop="indirectScale">
            <el-input-number v-model="form.indirectScale" :precision="2" :step="0.01" :max="100"
                             placeholder="请输入间推比例"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所在地区" prop="areaInfo">
            <el-cascader
              v-model="form.areaInfo"
              placeholder="请选择"
              :props="{label:'areaName',value:'id',checkStrictly: true}"
              :options="areaInfo"
              v-on:change="onChangeArea"
              filterable></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入详细地址"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="form.status"
              placeholder="请选择"
              clearable
              size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import operationModule from '@/mixins/operation-module'
  import {
    addAgent_user,
    updateAgent_user
  } from "@/api/agent/agent_user";

  export default {
    name: 'add-or-update',
    mixins: [operationModule],
    data() {
      return {
        // 设置属性
        mixinOperationModuleOptions: {
          saveURL: addAgent_user, // 提交数据接口，api地址
          updateURL: updateAgent_user // 提交数据接口，api地址
        },
        moduleName: "Agent_user",
        open: false,
        // 弹出层标题
        //表单操作类型
        formType: this.$formType.add,
        statusOptions: [],
        agentLevelOptions: [],
        areaInfo: [],
        initPassword: "",
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    methods: {
      init(type) {
        this.reset()
        this.open = true
        if (type == this.$formType.add) {
        } else if (type == this.$formType.edit) {
        } else if (type == this.$formType.detail) {
        }
        this.form.password = this.initPassword
        this.formType = type
      },
      onShow() {
      },
      onChangeArea(value) {
        if (value != null) {
          this.form.provinceId = value[0];
          this.form.cityId = value[1];
          this.form.districtId = value[2];
        }
        this.form.areaInfo = value;
      },
      // 表单重置
      reset() {
        this.form = {
          remark: undefined,
          status: "0",
          agentLevel: undefined,
          cityId: undefined,
          agentName: undefined,
          phone: undefined,
          createTime: undefined,
          districtId: undefined,
          createBy: undefined,
          username: undefined,
          password: undefined,
          provinceId: undefined,
          updateBy: undefined,
          inviteCode: undefined,
          directScale: undefined,
          id: undefined,
          chain: undefined,
          indirectScale: undefined,
          pid: undefined,
          areaInfo: undefined,
          address: undefined,
          updateTime: undefined
        }
        this.resetForm("form");
      },
      cancel() {
        this.$emit('on-close')
        this.open = false;
      }
    },
    created() {
      this.reset();
      this.getConfigKey('sys.user.initPassword').then(response => {
        this.initPassword = response.msg
      })
    }
  }
</script>
